package com.finace.money.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;

import java.util.List;

/**
 * Created by zhangpeng on 2018/2/1.
 */

public class AllCompanyAdapter extends BaseAdapter {

    private List<StatusListBean> showLists;

    public void setShowLists(List<StatusListBean> showLists) {
        this.showLists = showLists;
    }

    @Override
    public int getCount() {
        return null != showLists && showLists.size() > 6 ? 6 : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        CompanyViewHolder holder;
        if(null == view){
            holder = new CompanyViewHolder();
            view = LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_company, viewGroup, false);
            holder.tvCompanyChg = view.findViewById(R.id.tv_company_chg);
            holder.tvCompanyPrice = view.findViewById(R.id.tv_company_price);
            holder.tvCompanyNameSx = view.findViewById(R.id.tv_company_name_sx);
            holder.tvCompanyName = view.findViewById(R.id.tv_company_name);
            holder.tvCompanyTip = view.findViewById(R.id.tv_company_tip);
            view.setTag(holder);
        }else {
            holder = (CompanyViewHolder) view.getTag();
        }

        if(null != showLists && showLists.size() > 6){
            StatusListBean.MBodyBean m_body = showLists.get(position).getM_Body();
            if(null != m_body){
                switch (position) {
                    case 0:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorBlue4A90E2));
                        break;
                    case 1:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorYellowFF9800));
                        break;
                    case 2:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorGreen8BC34A));
                        break;
                    case 3:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorRedE51B23));
                        break;
                    case 4:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorYellowFF9800));
                        break;
                    case 5:
                        holder.tvCompanyNameSx.setBackgroundColor(UIUtil.getColor(R.color.colorBlue8089B3));
                        break;
                }
                holder.tvCompanyNameSx.setText(m_body.getM_ProductClassID());
                holder.tvCompanyName.setText(m_body.getM_ProductID());
                holder.tvCompanyTip.setText(m_body.getM_ExchangeName());

                TickBean tickbean = m_body.getTickbean();
                if(null != tickbean){
                    holder.tvCompanyPrice.setText(tickbean.getTrade());
                    if(Double.valueOf(tickbean.getTrade()) > Double.valueOf(m_body.getM_PreSettle().getM_Value())){
                        holder.tvCompanyPrice.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                    }else {
                        holder.tvCompanyPrice.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                    }
                    double zhangdie = Double.valueOf(tickbean.getTrade()) - m_body.getM_PreSettle().getM_Value();
                    holder.tvCompanyChg.setText(StringUtil.getNumberFormat(String.valueOf(zhangdie / m_body.getM_PreSettle().getM_Value())));
                }else {
                    holder.tvCompanyPrice.setTextColor(UIUtil.getColor(R.color.colorWhite));
                    holder.tvCompanyChg.setText("-");
                }

            }
        }

        return view;
    }

    public class CompanyViewHolder{

        TextView tvCompanyChg;
        TextView tvCompanyPrice;
        TextView tvCompanyNameSx;
        TextView tvCompanyName;
        TextView tvCompanyTip;

    }
}
